package com.kuangjia.conferenceroomrental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuangjia.conferenceroomrental.entity.Room;
import com.kuangjia.conferenceroomrental.mapper.RoomMapper;
import com.kuangjia.conferenceroomrental.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会议室服务实现类
 */
@Service
public class RoomServiceImpl implements RoomService {
    
    @Autowired
    private RoomMapper roomMapper;
    
    @Override
    @Transactional
    public Room createRoom(Room room) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        room.setCreatedAt(now);
        room.setUpdatedAt(now);
        
        // 如果状态为空，默认为可用
        if (room.getStatus() == null) {
            room.setStatus("AVAILABLE");
        }
        
        roomMapper.insert(room);
        return room;
    }
    
    @Override
    public Room getRoomById(Long id) {
        return roomMapper.selectById(id);
    }
    
    @Override
    public List<Room> getAllRooms() {
        return roomMapper.selectList(null);
    }
    
    @Override
    public IPage<Room> getRoomsByPage(Page<Room> page) {
        return roomMapper.selectPage(page, null);
    }
    
    @Override
    public List<Room> getAvailableRooms() {
        return roomMapper.selectAvailable();
    }
    
    @Override
    public IPage<Room> getAvailableRoomsByPage(Page<Room> page) {
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Room::getStatus, "AVAILABLE");
        return roomMapper.selectPage(page, queryWrapper);
    }
    
    @Override
    public List<Room> getRoomsByCapacity(Integer capacity) {
        return roomMapper.selectByCapacity(capacity);
    }
    
    @Override
    public IPage<Room> getRoomsByCapacityPage(Page<Room> page, Integer capacity) {
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Room::getCapacity, capacity);
        return roomMapper.selectPage(page, queryWrapper);
    }
    
    @Override
    @Transactional
    public Room updateRoom(Room room) {
        // 设置更新时间
        room.setUpdatedAt(LocalDateTime.now());
        roomMapper.updateById(room);
        return room;
    }
    
    @Override
    @Transactional
    public void deleteRoom(Long id) {
        roomMapper.deleteById(id);
    }
} 